;(function($, window) {
	var rgbaRegex = /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
	var getColor = function(colorStr) {
		var matches = colorStr.match(rgbaRegex);
		if(matches && matches.length === 5) {
			return [
				matches[1],
				matches[2],
				matches[3],
				matches[4]
			];
		}
		return [];
	};
	var Transparent = function(element, options) {
		this.element = element;
		this.options = $.extend({
			top: 0,
			offset: 150,
			duration: 16
		}, options || {});
		this._style = this.element.style;
		this._bgColor = this._style.backgroundColor;
		var color = getColor(mui.getStyles(this.element, 'backgroundColor'));
		if(color.length) {
			this._R = color[0];
			this._G = color[1];
			this._B = color[2];
			this._A = color[3];
			this._bufferFn = $.buffer(this.handleScroll, this.options.duration, this);
			this.initEvent();
		} else {
			throw new Error("元素背景颜色必须为RGBA");
		}
	};

	Transparent.prototype.initEvent = function() {
		window.addEventListener('scroll', this._bufferFn);
		window.addEventListener($.EVENT_MOVE, this._bufferFn);
	};
	Transparent.prototype.handleScroll = function() {
		if(typeof this.options.scroll === 'undefined') {
			var a = (window.scrollY - this.options.top) / this.options.offset;
			if(a > 0.85) {
				a = 1;
			}
			this.options.success(this, a);
			this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + a + ')';
		} else {
			var a = (this.options.top - this.options.scroll.y) / this.options.offset;
			if(a > .9) {
				a = 1;
			}
			this.options.success(this, a);
			 if( this.options.bgr && this.options.top - this.options.scroll.y > 30  ){
//	        		this.element.style.width = '100%';
	        		this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + a + ')';
	        }else if( this.options.bgr && this.options.top - this.options.scroll.y < 30  ){
//	        		this.element.style.width = '20%';
	        		this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + a + ')';
	        }else{
	        		this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + a + ')';
	        }
//			this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + a + ')';
		}
	};
	Transparent.prototype.destory = function() { 
		window.removeEventListener('scroll', this._bufferFn);
		window.removeEventListener($.EVENT_MOVE, this._bufferFn);
		this.element.style.backgroundColor = this._bgColor;
		this.element.mui_plugin_transparent = null;
	};
	$.fn.transparent2 = function(options) {
		options = options || {};
		var transparentApis = [];
		this.each(function() {
			var transparentApi = this.mui_plugin_transparent;
			if(!transparentApi) {

				
				var top = this.getAttribute('data-top');
				var offset = this.getAttribute('data-offset');
				var duration = this.getAttribute('data-duration');
				if(top !== null && typeof options.top === 'undefined') {
					options.top = top;
				}
				if(offset !== null && typeof options.offset === 'undefined') {
					options.offset = offset;
				}
				if(duration !== null && typeof options.duration === 'undefined') {
					options.duration = duration;
				}
				transparentApi = this.mui_plugin_transparent = new Transparent(this, options);
			}
			
			transparentApis.push(transparentApi);
		});

		
		return transparentApis.length === 1 ? transparentApis[0] : transparentApis;
	};
})(mui, window);


//评分
;(function($, W) {
	var defautls = {
		max:10,  //总分成积，
		star: 5 , // 显示的星星数量，
		cover: 0, //默认分数，
		success:function(){},
		eventstar:true,  // 允许评分
		fullStar:'../icon/icon_xingxing@2x.png',  //满星
		halfStar:'../icon/icon_bankexin@2x.png',  //半星
		voidStar:'../icon/icon_void@2x.png'   // 空星
	},prev = function(target, nodeName) {
		var prevNodes = [];
		var node = target.previousSibling;
		while( node ){
			if( node.nodeName.toLowerCase() == nodeName ){
				prevNodes.push( node );
			}
			node = node.previousSibling;
		}
		return prevNodes;
	},next = function(target, nodeName) {
		var nextNode = [];
		var node = target.nextSibling;
		while(node){
			if( node.nodeName.toLowerCase() == nodeName ){
				nextNode.push(node);
			}
			node = node.nextSibling;
		}
		return nextNode;
	};
	
	var score = function(element, options){
		this.ele = element;
		this.opt = $.extend(true,defautls, options || {});
		this.init();
	};
	score.prototype.init = function(){
		this.opt.cover = this.opt.cover/(this.opt.max/this.opt.star);
		this.showStar();
		if( this.opt.eventstar ){
			this.handle();
		}
	};
	score.prototype.showStar = function() {
		var intStar = parseInt( this.opt.cover );
//		console.log(this.opt.cover)
		this.ele.innerHTML = '';  // 清空div
		var htmlStr = '';
		for( var index = 0 ; index < intStar; index++ ){
			htmlStr += '<img src="'+this.opt.fullStar+'" />';
		}
		var overStarNum = this.opt.star - intStar;
		if( (this.opt.cover - intStar) > 0 ){
			htmlStr += '<img src="'+this.opt.halfStar+'" />';
			overStarNum -= 1;
		}
		for( var i = 0 ; i < overStarNum ; i++ ){
			htmlStr += '<img src="'+this.opt.voidStar+'" />';
		}
		this.ele.innerHTML = htmlStr;
	};
	score.prototype.handle = function(){
		var self = this;
		$(this.ele).on('tap', 'img', function(){
			var item = 1;
			$.each(prev(this, 'img'), function(){
				item++;
				this.src = self.opt.fullStar;
			});
			$.each(next(this, 'img'), function() {
				this.src = self.opt.voidStar;
			});
			this.src = self.opt.fullStar;
			this.parentNode.setAttribute('data-count', item);
			self.opt.success(this.parentNode);
		});
	};
	$.fn.score = function(options){
		options = options || {};
//		console.log( "AAA=" + bexta.jsonToString(options) )
		this.each(function() {
			options['cover'] = this.getAttribute('data-cover') || defautls.cover;
//			options['eventstar'] = this.getAttribute('data-eventstar') || defautls.eventstar; 
//			options['max'] = this.getAttribute('data-max') || defautls.max;
//			options['star'] = this.getAttribute('data-star') || defautls.star;
			
			new score( this, options );
		});
	}
})(mui, window);
